# app/main.py

import uvicorn
from fastapi import FastAPI
from starlette.staticfiles import StaticFiles
from api.routes import pageRoutes
from api.routes import message
from api.routes import login
from db.database_utils import DatabaseUtils
from model.Config import Config


app = FastAPI()
app.mount("/static", StaticFiles(directory="webpage"), name="static_files")
app.include_router(pageRoutes.router, prefix="/api")
app.include_router(message.router, prefix="/api")
app.include_router(login.router, prefix="/api")



async def startup_event():

    #     # 获取数据库连接
    db_utils = DatabaseUtils("mysql+pymysql://root:123456@"+Config.instance.get_db_host()+":3306/?charset=utf8mb4")
    # 创建数据库
    database = db_utils.create_database()
    # 获取数据库新连接
    db_utils = DatabaseUtils(f"mysql+pymysql://root:123456@"+Config.instance.get_db_host()+":3306/weixin?charset=utf8mb4")
    # 创建表
    db_utils.init_tables()


async def shutdown_event():
    # 在这里写你的关闭代码
    pass

app.router.add_event_handler("startup", startup_event)
app.router.add_event_handler("shutdown", shutdown_event)

if __name__ == "__main__":
    print("启动服务...")
    uvicorn.run("main:app", host="0.0.0.0", port=8000)
